package com.accloud.cloudservice;


import com.accloud.activator.HFV8Activator;
import com.accloud.activator.QCLTLinkActivator;
import com.accloud.service.ACDeviceBind;
import com.accloud.service.ACDeviceFind;
import com.accloud.service.ACException;
import com.accloud.service.ACWifiInfo;
import com.accloud.service.Receiver;
import com.accloud.utils.NetworkUtils;
import cz.msebera.android.httpclient.HttpStatus;
import java.util.List;
import org.json.JSONException;

public abstract class ACDeviceActivator {
    public static final int AI6060H = 11;
    public static final int AP = -2;
    public static final int BL = 13;
    public static final int ESP8266 = 9;
    public static final int HF = 1;
    public static final int HF_V3 = 16;
    public static final int HF_V8 = 17;
    public static final int MILL = 12;
    public static final int MX = 2;
    public static final int MX_LEGACY = 3;
    public static final int QCLTLINK = 15;
    public static final int QCSNIFFER = 4;
    public static final int REALTEK = 10;
    public static final String TAG = "ACDeviceActivator";
    private AbleLinkingFind ableFind;
    private boolean isAbleLink = false;

    public abstract void startSmartConfig(String str, String str2, int i);

    public static ACDeviceActivator of(int type) {
        switch (type) {
            case -2:
                return new ACDeviceActivator() {
                    /* class com.accloud.cloudservice.ACDeviceActivator.AnonymousClass1 */

                    @Override // com.accloud.cloudservice.ACDeviceActivator
                    public void startSmartConfig(String ssid, String password, int timeout_ms) {
                        throw new RuntimeException("ACDeviceActivator.AP does not support Ablelink");
                    }
                };
            case -1:
            case 0:
            case 5:
            case 6:
            case 7:
            case 8:
            case 14:
            default:
                return null;
            case 15:
                return new QCLTLinkActivator();
            case 17:
                return new HFV8Activator();
        }
    }

    public String getSSID() {
        return NetworkUtils.getSSID(AC.context);
    }

    public String getBssid() {
        return NetworkUtils.getBSSID(AC.context);
    }

    public boolean isAbleLink() {
        return this.isAbleLink;
    }

    public void startAbleLink(final String SSID, final String password, int timeout, final PayloadCallback<ACDeviceBind> callback) {
        this.isAbleLink = true;
        AC.deviceLocalManager.findLinkingDevice(timeout, HttpStatus.SC_INTERNAL_SERVER_ERROR, new PayloadCallback<ACDeviceFind>() {
            /* class com.accloud.cloudservice.ACDeviceActivator.AnonymousClass2 */

            public void success(ACDeviceFind acDeviceFind) {
                AC.sEventManager.track(new FindDeviceEvent(SSID, password, 1,null));
                callback.success(new ACDeviceBind(acDeviceFind.getSubDomainId(), acDeviceFind.getPhysicalDeviceId()));
            }

            @Override // com.accloud.cloudservice.BaseCallback
            public void error(ACException e) {
                AC.sEventManager.track(new FindDeviceEvent(SSID, password, 0, e));
                callback.error(e);
            }
        });
        startSmartConfig(SSID, password, timeout);
        AC.sEventManager.track(new BeginLinkEvent(SSID, password));
    }

    public void startAbleLink(final String SSID, final String password, final String physicalDeviceId, int timeout, final PayloadCallback<ACDeviceBind> callback) {
        this.isAbleLink = true;
        this.ableFind = new AbleLinkingFind(timeout, HttpStatus.SC_INTERNAL_SERVER_ERROR, new Receiver<ACDeviceFind>() {
            /* class com.accloud.cloudservice.ACDeviceActivator.AnonymousClass3 */

            public void onReceive(ACDeviceFind localDevice) {
                if (physicalDeviceId.equals(localDevice.getPhysicalDeviceId())) {
                    ACDeviceActivator.this.stopFind();
                    AC.sEventManager.track(new FindDeviceEvent(SSID, password, 1));
                    callback.success(new ACDeviceBind(localDevice.getSubDomainId(), localDevice.getPhysicalDeviceId()));
                }
            }
        });
        this.ableFind.execute(new VoidCallback() {
            /* class com.accloud.cloudservice.ACDeviceActivator.AnonymousClass4 */

            @Override // com.accloud.cloudservice.VoidCallback
            public void success() {
                ACDeviceActivator.this.stopFind();
            }

            @Override // com.accloud.cloudservice.BaseCallback
            public void error(ACException error) {
                ACDeviceActivator.this.stopFind();
                AC.sEventManager.track(new FindDeviceEvent(SSID, password, 0, error));
                callback.error(error);
            }
        });
        startSmartConfig(SSID, password, timeout);
        AC.sEventManager.track(new BeginLinkEvent(SSID, password));
    }

    public void startApLink(final String SSID, final String password, final int timeout, final PayloadCallback<Boolean> linkCallback, final PayloadCallback<ACDeviceBind> callback) {
        final Runnable findLinkTask = new Runnable() {
            /* class com.accloud.cloudservice.ACDeviceActivator.AnonymousClass5 */

            @Override // java.lang.Runnable
            public void run() {
                int countDown = timeout;
                while (true) {
                    if (countDown <= 0) {
                        break;
                    } else if (SSID.equals(ACDeviceActivator.this.getSSID())) {
                        try {
                            Thread.sleep(3000);
                            break;
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    } else {
                        try {
                            Thread.sleep(1000);
                        } catch (InterruptedException e2) {
                            e2.printStackTrace();
                        }
                    }
                }
                ACException timeoutException = new ACException(ACException.TIMEOUT, "timeout");
                if (countDown <= 0) {
                    callback.error(timeoutException);
                } else {
                    AC.deviceLocalManager.findLinkingDevice(countDown, HttpStatus.SC_INTERNAL_SERVER_ERROR, new PayloadCallback<ACDeviceFind>() {
                        /* class com.accloud.cloudservice.ACDeviceActivator.AnonymousClass5.AnonymousClass1 */

                        public void success(ACDeviceFind acDeviceFind) {
                            AC.sEventManager.track(new FindDeviceEvent(SSID, password, 1));
                            callback.success(new ACDeviceBind(acDeviceFind.getSubDomainId(), acDeviceFind.getPhysicalDeviceId()));
                        }

                        @Override // com.accloud.cloudservice.BaseCallback
                        public void error(ACException e) {
                            AC.sEventManager.track(new FindDeviceEvent(SSID, password, 0, e));
                            callback.error(e);
                        }
                    });
                }
            }
        };
        AC.deviceLocalManager.setWifiToAP(SSID, password, AC.SEND_TO_LOCAL_DEVICE_DEFAULT_TIMEOUT, new VoidCallback() {
            /* class com.accloud.cloudservice.ACDeviceActivator.AnonymousClass6 */

            @Override // com.accloud.cloudservice.VoidCallback
            public void success() {
                if (linkCallback != null) {
                    linkCallback.success(true);
                }
                new Thread(findLinkTask).start();
                AC.sEventManager.track(new BeginLinkEvent(SSID, password));
            }

            @Override // com.accloud.cloudservice.BaseCallback
            public void error(ACException e) {
                if (linkCallback != null) {
                    linkCallback.error(e);
                }
            }
        });
    }

    public void searchAvailableWifi(int timeout, PayloadCallback<List<ACWifiInfo>> callback) {
        AC.deviceLocalManager.getWifiFromAP(timeout, callback);
    }

    public void stopAbleLink() {
        AC.deviceLocalManager.stopFind();
        stopSmartConfig();
    }

    /* access modifiers changed from: package-private */
    public void stopFind() {
        if (this.ableFind != null) {
            this.ableFind.cancel();
        }
    }

    public void stopSmartConfig() {
        this.isAbleLink = false;
    }

    private class BeginLinkEvent extends ACEvent {
        private BeginLinkEvent(String ssid, String password) {
            super("apm_app_dist_network", "begin", null);
            String encoded;
            try {
                this.data.put("ssid", ssid);
                if (password == null || password.length() == 0) {
                    encoded = "";
                } else {
                    encoded = password.charAt(0) + "******" + password.charAt(password.length() - 1);
                }
                this.data.put("password", encoded);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    /* access modifiers changed from: private */
    public class FindDeviceEvent extends ACEvent {
        private FindDeviceEvent(ACDeviceActivator aCDeviceActivator, String ssid, String password, int count) {
            this(ssid, password, count, (ACException) null);
        }

        private FindDeviceEvent(String ssid, String password, int count, ACException exception) {
            super("apm_app_dist_network", "find", exception);
            String encoded;
            try {
                this.data.put("ssid", ssid);
                if (password == null || password.length() == 0) {
                    encoded = "";
                } else {
                    encoded = password.charAt(0) + "******" + password.charAt(password.length() - 1);
                }
                this.data.put("password", encoded);
                this.data.put("count", count);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        private FindDeviceEvent(String ssid, String password, int count) {
            super("apm_app_dist_network", "find", null);
            String encoded;
            try {
                this.data.put("ssid", ssid);
                if (password == null || password.length() == 0) {
                    encoded = "";
                } else {
                    encoded = password.charAt(0) + "******" + password.charAt(password.length() - 1);
                }
                this.data.put("password", encoded);
                this.data.put("count", count);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}
